Building an Email Mechanism Using AWS Lambda and SES
Introduction
Hello, I’m Hemanth from the Alliance Department. In this blog, I will guide you through the step-by-step process of setting up an email-sending mechanism using AWS Lambda and Amazon Simple Email Service (SES). This is useful for automating email notifications or alerts directly from AWS Lambda in a serverless, scalable, and cost-efficient manner.
AWS
Amazon Web Services, or AWS, is a cloud service platform that provides content distribution, database storage, processing capacity, and other features to support corporate expansion. AWS has offered a broad range of services in many different categories, including Compute, Storage, Networking, Database, Management Tools, and Security.
AWS Lambda
A serverless compute service which runs code as a reply to events and automatically takes care of the bottom resources. It runs code on high availability compute infrastructure and performs all the administration of the compute resources. A few examples are HTTP requests via Amazon API Gateway, changes to objects in S3, and many others.
AWS SES
A cloud-based email sending solution called Amazon SES is intended for both transactional and bulk email sending. It can be combined with Lambda and other AWS services to send emails automatically in response to predefined triggers. For developers, SES offers scalable, dependable email delivery infrastructure.
Demo
Verify Sender and Recipient Email Addresses in Amazon SES. Go to the AWS Management Console, search for SES, and click on Create Identity.
In the identity details, select Email Address, and then click Create Identity.
Repeat the same steps to verify the recipient’s email address.
Note: In SES’s sandbox environment, both sender and receiver email addresses must be verified. In production, only the sender address needs to be verified.
Create SMTP Credentials for SES. Under SES, go to SMTP Settings and create SMTP credentials. Provide a username and click Create User. Download the SMTP credentials file, as this information will be required when setting up Lambda.
Create an IAM Role for Lambda. Navigate to the IAM Console and create a new role. Choose Lambda as the service and assign AmazonSESFullAccess permissions. Click Create Role.
Create an AWS Lambda Function. In the AWS Management Console, search for Lambda and click Create Function. Provide a function name, select Python as the runtime, and assign the IAM role you created earlier.
Paste the following code into the Lambda function editor. Be sure to replace the placeholders with the SMTP credentials and verified email addresses. Once the code is in place, click Deploy.
import smtplib
from email.mime.text import MIMEText
def lambda_handler(event, context):
smtp_username = "usrname"
smtp_password = "passwd"
smtp_host = "host"
smtp_port = 465
sender = "senderaddress"
recipient = "receipientaddress"
subject = "Test Email from AWS Lambda"
body = "This is a test email sent using AWS Lambda and SES."
#Create message
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = recipient
#Connect to SMTP server and send email
try:
server = smtplib.SMTP_SSL(smtp_host, smtp_port)
server.login(smtp_username, smtp_password)
server.sendmail(sender, recipient, msg.as_string())
server.quit()
return {'statusCode': 200, 'body': 'Email sent successfully!'}
except Exception as e:
return {'statusCode': 500, 'body': str(e)}
Create a test event and run the Lambda function. You should receive a success message and a test email in your recipient’s inbox.
Conclusion
By following this guide, you now have a fully functioning email notification system built using AWS Lambda and SES. This serverless email mechanism can be used in various applications, from sending transactional emails to alert notifications. It eliminates the need for maintaining infrastructure while providing high scalability.